const router = require('koa-router')()
const { loginRedirect, loginCheck } = require('../midlewares/loginChecks')

router.get('/', loginRedirect, async (ctx, next) => {
  // console.log("3-10 浏览器调试前...");
  // debugger
  // console.log("3-10 正在浏览器调试...");
  await ctx.render('index', {
    title: 'Hello Koa 2!',
    msg: '你好!',
    isMe: false,
    blogList:[{
      id: 1,
      title: 'aaa'
    },{
      id: 2,
      title: 'bbb'
    },{
      id: 3,
      title: 'ccc'
    },{
      id: 4,
      title: 'ddd'
    }]
  })
})

// router.get('/string', async (ctx, next) => {
//   ctx.body = 'koa2 string'
// })

router.get('/json', loginCheck, async (ctx, next) => {
  // 使用ctx.session获取当前用户进入该路由的动态会话session
/*  const session = ctx.session;
  if (session.viewNum == null) {
      session.viewNum = 0 // 第一次进来没有，赋值为0
  }

  session.viewNum++
*/
//  throw Error()
  ctx.body = {
    title: 'koa2 json'
    // viewNum: session.viewNum
  }
})

router.get('/profile/:userName', async(ctx, next)=>{
  const { userName } = ctx.params
  ctx.body = {
    title: 'this is profile page',
    userName
  }
})

router.get('/loadMore/:userName/:pageIndex', async (ctx, next) =>{
  const { userName,pageIndex } = ctx.params
  ctx.body = {
    title: 'this is loadMore API',
    userName,
    pageIndex
  }
})

module.exports = router
